﻿// <copyright file="Utilities.cs">
//  Copyright (c) 2008 Eric Patey
//
//  Permission is hereby granted, free of charge, to any person
//  obtaining a copy of this software and associated documentation
//  files (the "Software"), to deal in the Software without
//  restriction, including without limitation the rights to use,
//  copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following
//  conditions:
//
//  The above copyright notice and this permission notice shall be
//  included in all copies or substantial portions of the Software.
//
//  THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//  OTHER DEALINGS IN THE SOFTWARE.
// </copyright>

namespace WaringAuction
{
    using System;
    using System.IO;
    using System.Web;
    using EricUtilities;
    using Microsoft.Samples.ServiceHosting.StorageClient;

    public static class Utilities
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "6#", Justification = "This is how I want it."), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "5#", Justification = "This is how I want it.")]
        public static void CreateFileBlobAndThumbnail(
            HttpPostedFileBase file,
            BlobContainer blobContainer,
            int thumbnailWidth,
            int thumbnailHeight,
            bool cropForDesiredAspectRatio,
            out string fullSizeName,
            out string thumbnailName)
        {
            thumbnailName = null;
            fullSizeName = CreateFileBlob(file, blobContainer);
            thumbnailName = CreateThumbnail(blobContainer, fullSizeName, thumbnailWidth, thumbnailHeight, cropForDesiredAspectRatio);
        }

        public static string CreateThumbnail(
            BlobContainer blobContainer,
            string fullsizeBlobName,
            int thumbnailWidth,
            int thumbnailHeight,
            bool cropForDesiredAspectRatio)
        {
            MemoryStream ms = new MemoryStream();
            BlobContents contents = new BlobContents(ms);
            blobContainer.GetBlob(fullsizeBlobName, contents, false);
            Stream thumbnailStream = ThumbnailHelper.CreateThumbnail(ms, thumbnailWidth, thumbnailHeight, cropForDesiredAspectRatio);

            return CreateFileBlob(thumbnailStream, "image/jpeg", blobContainer);
        }

        public static string CreateFileBlob(HttpPostedFileBase file, BlobContainer blobContainer)
        {
            using (Stream stream = file.InputStream)
            {
                return CreateFileBlob(stream, file.ContentType, blobContainer);
            }
        }

        public static string CreateFileBlob(Stream stream, string contentType, BlobContainer blobContainer)
        {
            BlobProperties props = new BlobProperties(Guid.NewGuid().ToString("N"))
            {
                ContentType = contentType
            };
            BlobContents contents = new BlobContents(stream);
            if (!blobContainer.CreateBlob(props, contents, false))
            {
                throw new InvalidOperationException("Couldn't create blob");
            }

            return props.Name;
        }
    }
}
